package jast;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import loader.FileClassLoader;
/**
 * Not Completed
 */
public class ProgExec {

	public static void main(String[] args) throws Exception {
		FileClassLoader fcl = new FileClassLoader();
		Class<?> tc = fcl.findClass("F:\\eclipse\\workspace\\AntlrJava\\bin\\test\\TestTarget.class", "test.TestTarget");
		Method[] tcMethods = tc.getDeclaredMethods();
		for(int i=0;i<tcMethods.length;i++){
			Object[] params = new Object[tcMethods[i].getParameterCount()];
			for(int j=0;j<tcMethods[i].getParameterCount();j++){
				String typeName = tcMethods[i].getParameterTypes()[j].getTypeName();
				if(tcMethods[i].getParameterTypes()[j].isArray()){
					//数组类型
					System.out.println(typeName);
					String sTypeName = typeName.substring(0, typeName.indexOf("["));
					if(sTypeName.equals("char"))
						params[j] = new char[4];
				}else if(tcMethods[i].getParameterTypes()[j].isPrimitive()){
					//基本类型
					System.out.println(typeName);
					if(typeName.equals("int"))
						params[j] = 3;
					else if(typeName.equals("char"))
						;
				}else{
					//非数组对象类型
					System.out.println(typeName);
					params[j] = tcMethods[i].getParameterTypes()[j].newInstance();
				}
//				Constructor<?>[] ctr = tcMethods[i].getParameterTypes()[j].getConstructors();
//				System.out.println(Integer.class.getConstructors().length);
//				for(int t=0;t<ctr.length;t++){
//					System.out.println(ctr[t].getParameterTypes().length);
//				}
			}
			Object obj = tc.newInstance();
			tcMethods[i].invoke(obj, params);
		}
	}

	/**
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	private static void formalMain()
			throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
		FileClassLoader fcl = new FileClassLoader();
		Class<?> tc = fcl.findClass("F:\\eclipse\\workspace\\AntlrJava\\bin\\test\\TestTarget.class", "test.TestTarget");
		Method[] tcMethods = tc.getDeclaredMethods();
		for(int i=0;i<tcMethods.length;i++){
			System.out.println(tcMethods[i].getName()+" "+tcMethods[i].getModifiers());
			for(int j=0;j<tcMethods[i].getParameterCount();j++)
				System.out.println(tcMethods[i].getParameterTypes()[j].getName());
			if(tcMethods[i].getName().equals("multiFunction")){
				Object obj = tc.newInstance();
				tcMethods[i].invoke(obj, 1,2,3,new char[4]);
			}
		}
	}
	
	public void exec(String name,String className){
		FileClassLoader fcl = new FileClassLoader();
		try {
			Class<?> tc = fcl.findClass(name, className);
			Method[] tcMethods = tc.getDeclaredMethods();
			for(int i=0;i<tcMethods.length;i++){
				exec(tc, tcMethods[i]);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void exec(Class<?> c, Method method){
		if(method.getName().equals("multiFunction")){
			Object obj;
			try {
				obj = c.newInstance();
				method.invoke(obj, 1,2,3,new char[4]);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			
		}
	}
}